Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bounty.go handlers GetAllBounties, GetBountyById, & GetBountyIndexById #1526 #1538

Merged
merged 25 commits into from
Feb 27, 2024

Conversation

gouravmpk
Copy link
Contributor

@gouravmpk gouravmpk commented Feb 20, 2024

Describe your changes

  • TestGetBountyById
  • TestGetAllBounties
  • GetBountyIndexById

Issue ticket number and link

Backend [Integration Test] bounty.go handlers GetAllBounties, GetBountyById, & GetBountyIndexById #1526

Type of change

Test

Checklist before requesting a review

  • I have performed a self-review of my code
  • I have tested on Chrome and Firefox
  • I have tested on a mobile device
  • I have provided a screenshot or recording of changes in my PR if there were updates to the frontend

@ecurrencyhodler
Copy link
Contributor

@gouravmpk all your tests have failed. Have you tried running the tests locally first?

@gouravmpk
Copy link
Contributor Author

gouravmpk commented Feb 20, 2024

Yes I have provided Snapshot in
#1537

@ecurrencyhodler
Copy link
Contributor

That looks like it's for a separate PR. Did you test locally for this specific pr?

@gouravmpk gouravmpk changed the title Tests#1526 bounty.go handlers GetAllBounties, GetBountyById, & GetBountyIndexById #1526 Feb 21, 2024
handlers/organizations.go Outdated Show resolved Hide resolved
handlers/bounty_test.go Outdated Show resolved Hide resolved
@elraphty
Copy link
Contributor

@gouravmpk you removed the test for getting all bounties

rename this to GetPersonAssigned Bounties, and create a test for GetAllBounties, thanks.

func TestGetAllBounties(t *testing.T) {

	ctx := context.Background()
	mockDb := dbMocks.NewDatabase(t)
	mockHttpClient := mocks.NewHttpClient(t)
	bHandler := NewBountyHandler(mockHttpClient, mockDb)
	t.Run("should return bounties assigned to the user", func(t *testing.T) {
		mockGenerateBountyResponse := func(bounties []db.Bounty) []db.BountyResponse {
			var bountyResponses []db.BountyResponse

			for _, bounty := range bounties {
				owner := db.Person{
					ID: 1,
				}
				assignee := db.Person{
					ID: 1,
				}
				organization := db.OrganizationShort{
					Uuid: "uuid",
				}

				bountyResponse := db.BountyResponse{
					Bounty:       bounty,
					Assignee:     assignee,
					Owner:        owner,
					Organization: organization,
				}
				bountyResponses = append(bountyResponses, bountyResponse)
			}

			return bountyResponses
		}
		bHandler.generateBountyResponse = mockGenerateBountyResponse

		expectedBounties := []db.Bounty{
			{ID: 1, Assignee: "user1"},
			{ID: 2, Assignee: "user1"},
		}

		mockDb.On("GetAssignedBounties", mock.Anything).Return(expectedBounties, nil).Once()
		mockDb.On("GetPersonByPubkey", mock.Anything).Return(db.Person{}, nil)
		mockDb.On("GetOrganizationByUuid", mock.Anything).Return(db.Organization{}, nil)
		rr := httptest.NewRecorder()
		req, err := http.NewRequest("GET", "/wanteds/assigned/uuid", nil)
		req = req.WithContext(ctx)
		if err != nil {
			t.Fatal(err)
		}

		bHandler.GetPersonAssignedBounties(rr, req)

		assert.Equal(t, http.StatusOK, rr.Code)

		var responseData []db.BountyResponse
		err = json.Unmarshal(rr.Body.Bytes(), &responseData)
		if err != nil {
			t.Fatalf("Error decoding JSON response: %s", err)
		}

		assert.NotEmpty(t, responseData)
		assert.Len(t, responseData, 2)

		for i, expectedBounty := range expectedBounties {
			assert.Equal(t, expectedBounty.ID, responseData[i].Bounty.ID)
			assert.Equal(t, expectedBounty.Assignee, responseData[i].Bounty.Assignee)
		}
	})

	t.Run("should not return bounties assigned to other users", func(t *testing.T) {
		mockGenerateBountyResponse := func(bounties []db.Bounty) []db.BountyResponse {
			return []db.BountyResponse{}
		}
		bHandler.generateBountyResponse = mockGenerateBountyResponse

		mockDb.On("GetAssignedBounties", mock.Anything).Return([]db.Bounty{}, nil).Once()

		rr := httptest.NewRecorder()
		req, err := http.NewRequest("GET", "/wanteds/assigned/uuid", nil)
		req = req.WithContext(ctx)
		if err != nil {
			t.Fatal(err)
		}

		bHandler.GetPersonAssignedBounties(rr, req)

		assert.Equal(t, http.StatusOK, rr.Code)

		var responseData []db.BountyResponse
		err = json.Unmarshal(rr.Body.Bytes(), &responseData)
		if err != nil {
			t.Fatalf("Error decoding JSON response: %s", err)
		}

		assert.Empty(t, responseData)
		assert.Len(t, responseData, 0)
	})
}

@gouravmpk gouravmpk requested a review from elraphty February 26, 2024 16:59
handlers/bounty_test.go Outdated Show resolved Hide resolved
handlers/bounty_test.go Outdated Show resolved Hide resolved
@gouravmpk gouravmpk requested a review from elraphty February 27, 2024 10:00
@elraphty elraphty merged commit fb8fd74 into stakwork:master Feb 27, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants